#include "StdAfx.h"
#include "Door.h"
#include "Level.h"
#include "CubePhysX.h"
#include "NxActor.h"
#include "Character.h"
#include "NxShape.h"
#include "Sphere.h"
#include "PhysX.h"
#include "DX10ObjMesh.h"
#include "ObjReader.h"
#include "D3DApp.h"

Door::Door(Level* pLevel,D3DXVECTOR3 location, float yRot, D3DApp* app):
	m_Location(location),
	m_YRot(yRot),
	m_Rotate(false),
	m_pDoorMesh(0),
	m_pTriggerMesh(0),
	m_pCollisionMesh(0),
	m_app(app)
{
	m_pLevel = pLevel;
	m_pCollisionMesh = new CubePhysX(m_pLevel,1,2,1,D3DXCOLOR(0,0,1,1),CubePhysX::RIGID);
	m_pCollisionMesh->Translate(location);
	m_pCollisionMesh->Rotate(D3DXVECTOR3(0,m_YRot,0));

	m_pTriggerMesh = new CubePhysX(m_pLevel,2.5,2.5,2.5,D3DXCOLOR(0,0,1,1),CubePhysX::GHOST);
	m_pTriggerMesh->Translate(location);
	m_pTriggerMesh->Rotate(D3DXVECTOR3(0,m_YRot,0));
	NxActor* actor = m_pTriggerMesh->GetActor();
	actor->userData = this;

	//Deel 2

	NxVec3 axisLocation(location.x - 2.5f/2,location.y + 2.5f/2,location.z);
	NxVec3 axisDirection(0,1,0);
	NxRevoluteJointDesc revDesc;
	revDesc.actor[0] = m_pCollisionMesh->GetActor();
	revDesc.setGlobalAxis(axisDirection);
	//The direction of the axis the bodies revolve around.
	revDesc.setGlobalAnchor(axisLocation);
	revDesc.name = "hinge";
	revDesc.motor.maxForce = 1000;
	revDesc.motor.velTarget = 0;
	revDesc.limit.low.value = 0.0f;
	revDesc.limit.high.value = NxPi/2.0f;
	revDesc.flags |= NX_RJF_MOTOR_ENABLED | NX_RJF_LIMIT_ENABLED;
	m_pHinge = (NxRevoluteJoint *)m_pLevel->GetPhysicsScene()->createJoint(revDesc);
}

Door::~Door(void)
{
	delete m_pDoorMesh;
	delete m_pTriggerMesh;
	delete m_pCollisionMesh;
}


void Door::Tick(const InputState& inputState)
{
	//m_pDoorMesh->SetWorldMatrix(m_pTriggerMesh->GetWorldMatrix());
}
	
void Door::Draw(const RenderContext* context, ID3D10ShaderResourceView* shadowdepthmap) {
	if ( m_pDoorMesh )
		m_pDoorMesh->Draw(context);
}

void Door::Initialize(ContentManager* manager)
{
	IniFile* config = new IniFile();
	config->SetValue(_T("Export"),_T("baseDir"),_T("."));
	config->SetValue(_T("Export"),_T("meshDir"),_T(""));
	config->SetValue(_T("Export"),_T("textureDir"),_T(""));
	config->SetValue(_T("Export"),_T("skeletonDir"),_T(""));

	ObjReader reader(config,_T("./Models/Door/door.obj"));
	if ( reader.Read() )
	{
		MeshList* list = new MeshList();
		reader.CopyIntoMeshList(list);
		vector<ObjMesh*>::const_iterator it;
		for (it=list->begin(); it!= list->end(); it++)
		{
			ObjMesh* mesh = *it;
			// standaard shader meegeven
			DX10ObjMesh* dx10Mesh = m_app->Compile(mesh,_T("NormalMap.fx"), _T("checkertech"));
			if ( dx10Mesh != 0 )
			{
				m_pDoorMesh = dx10Mesh;
			}
		}
	}
	m_pDoorMesh->SetParent(m_pLevel);
	m_pDoorMesh->Initialize(manager);
	m_pDoorMesh->Rotate(D3DXVECTOR3(1.5f,0,0));
	m_pDoorMesh->Translate(m_Location);

	m_pTriggerMesh = new CubePhysX(m_pLevel, 1.5f, 1.5f, 1.5f,D3DXCOLOR(1,1,1,1),CubePhysX::GHOST);
	m_pTriggerMesh->Translate(m_Location);

	m_pTriggerMesh->GetActor()->userData = this;
}

void Door::OnTriggerEnter(Character* player)
{
	if ( player->HasKey(1) )
	{
		NxMotorDesc desc;
		desc.velTarget= 1.20f;
		desc.maxForce = 1000;
		m_pHinge->setMotor(desc);
	}
}

void Door::OnTriggerExit(Character* player)
{
	if (player->HasKey(1)) 
	{
		NxMotorDesc desc;
		desc.velTarget= -1.20f;
		desc.maxForce = 1000;
		m_pHinge->setMotor(desc);
	}
}

void Door::DisableTrigger()
{

}